package com.jackstraw.jack_base.datasource.common.contextholder;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.jackstraw.jack_base.datasource.common.entity.enums.DataSourceTypeEnum;
import com.jackstraw.jack_base.datasource.common.entity.enums.RWEnum;
import com.jackstraw.jack_base.util.ObjectUtil;
import com.jackstraw.jack_base.util.StringUtil;

/**
 * 对数据源的ContextHolder
 */
public class DataSourceContextHolder {

    /**
     * db
     */
    private static final ThreadLocal<String> dbMainNameContextHolder = new TransmittableThreadLocal<>();

    /**
     * redis
     */
    private static final ThreadLocal<String> redisNameContextHolder = new TransmittableThreadLocal<>();

    /**
     * es
     */
    private static final ThreadLocal<String> esNameContextHolder = new TransmittableThreadLocal<>();

    /**
     * zookeeper
     */
    private static final ThreadLocal<String> zookeeperNameContextHolder = new TransmittableThreadLocal<>();


    /**
     * 清空dbContextHolder
     */
    public static void clearDb() {
        dbMainNameContextHolder.remove();
    }


    /**
     * 设置dbContextHolder
     *
     * @param dbMainName 数据源主名称
     */
    public static void setDb(String dbMainName) {
        if (StringUtil.StringUtilsBylang3.isNotEmpty(dbMainName)) {
            dbMainNameContextHolder.set(dbMainName);
        }
    }


    /**
     * 得到dbContextHolder的数据源主名称
     */
    public static String getDbMainName() {
        return dbMainNameContextHolder.get();
    }

    /**
     * 清空redisContextHolder
     */
    public static void clearRedis() {
        redisNameContextHolder.remove();
    }


    /**
     * 设置redisContextHolder
     *
     * @param redisName redis数据源名称
     */
    public static void setRedis(String redisName) {
        if (StringUtil.StringUtilsBylang3.isNotEmpty(redisName)) {
            redisNameContextHolder.set(redisName);
        }
    }

    /**
     * 得到redisContextHolder数据源名称
     */
    public static String getRedis() {
        return redisNameContextHolder.get();
    }


    /**
     * 清空esContextHolder
     */
    public static void clearEs() {
        esNameContextHolder.remove();
    }


    /**
     * 设置esContextHolder
     *
     * @param esName es数据源名称
     */
    public static void setEs(String esName) {
        if (StringUtil.StringUtilsBylang3.isNotEmpty(esName)) {
            esNameContextHolder.set(esName);
        }
    }

    /**
     * 得到esContextHolder数据源名称
     */
    public static String getEs() {
        return esNameContextHolder.get();
    }

    /**
     * 清空zookeeperContextHolder
     */
    public static void clearZookeeper() {
        zookeeperNameContextHolder.remove();
    }


    /**
     * 设置zookeeperContextHolder
     *
     * @param zookeeperName zookeeper数据源名称
     */
    public static void setZookeeper(String zookeeperName) {
        if (StringUtil.StringUtilsBylang3.isNotEmpty(zookeeperName)) {
            zookeeperNameContextHolder.set(zookeeperName);
        }
    }

    /**
     * 得到zookeeperContextHolder数据源名称
     */
    public static String getZookeeper() {
        return zookeeperNameContextHolder.get();
    }

    /**
     * 设置当前线程下不同数据源类型的数据源名称
     *
     * @param dataSourceTypeEnum
     * @param dataSourceName
     */
    public static void setDataSourceName(DataSourceTypeEnum dataSourceTypeEnum, String dataSourceName) {
        switch (dataSourceTypeEnum) {
            case redis:
                DataSourceContextHolder.setRedis(dataSourceName);
                break;
            case database:
                DataSourceContextHolder.setDb(dataSourceName);
                break;
            case elasticsearch:
                DataSourceContextHolder.setEs(dataSourceName);
                break;
            case zookeeper:
                DataSourceContextHolder.setZookeeper(dataSourceName);
                break;
        }
    }

    /**
     * 通过数据源类型获取当前线程数据源名称
     *
     * @param dataSourceTypeEnum
     * @return
     */
    public static String getDataSourceName(DataSourceTypeEnum dataSourceTypeEnum) {
        switch (dataSourceTypeEnum) {
            case redis:
                return DataSourceContextHolder.getRedis();
            case database:
                return DataSourceContextHolder.getDbMainName();
            case elasticsearch:
                return DataSourceContextHolder.getEs();
            case zookeeper:
                return DataSourceContextHolder.getZookeeper();
            default:
                return "";
        }
    }


    /**
     * 清除当前线程下不同数据源类型的数据源名称
     *
     * @param dataSourceTypeEnum
     */
    public static void clearDataSourceName(DataSourceTypeEnum dataSourceTypeEnum) {
        switch (dataSourceTypeEnum) {
            case redis:
                DataSourceContextHolder.clearRedis();
                break;
            case database:
                DataSourceContextHolder.clearDb();
                break;
            case elasticsearch:
                DataSourceContextHolder.clearEs();
                break;
            case zookeeper:
                DataSourceContextHolder.clearZookeeper();
                break;
        }
    }


}
